package com.yanqu.road.server.cmd.activity.hsisland;

import com.yanqu.road.entity.activity.hsisland.HsIslandChooseResult;
import com.yanqu.road.entity.activity.hsisland.HsIslandEventReward;
import com.yanqu.road.entity.activity.hsisland.HsIslandPvpBattleResult;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.HsIslandProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandActivity;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandActivityMgr;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandUser;
import com.yanqu.road.server.manager.activity.hsisland.pb.CrossHsIslandActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_HS_ISLAND_DO_EVENT_CHOOSE_REWARD, desc = "海神岛-事件处理-2选1事件")
public class CrossHsIslandDoChooseCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        HsIslandProto.HsIslandEventChooseRewardReqMsg reqMsg = HsIslandProto.HsIslandEventChooseRewardReqMsg.parseFrom(packet.getBytes());
        HsIslandProto.HsIslandEventChooseRewardRespMsg.Builder respMsg = HsIslandProto.HsIslandEventChooseRewardRespMsg.newBuilder();
        CrossHsIslandActivity activity = CrossHsIslandActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_DO_EVENT_CHOOSE_REWARD, respMsg));
            return;
        }
        try {
            //2选1
            HsIslandChooseResult result = activity.doChooseEvent(packet.getPlayerId(), reqMsg.getIslandId(), reqMsg.getPosition().getX(), reqMsg.getPosition().getY(), reqMsg.getIndex());
            HsIslandEventReward hsIslandEventReward = result.getHsIslandEventReward();
            //构造PB
            CrossHsIslandUser user = activity.getUser(packet.getPlayerId());
            //添加打理奖励
            if (reqMsg.getAutoFlag()) {
                if (hsIslandEventReward.getReward() != null) {
                    user.addAutoReward(hsIslandEventReward.getReward().getReward());
                }
            }
            respMsg.setRet(0);
            respMsg.setUserData(CrossHsIslandActivityPb.buildHsIslandUserData(user));
            respMsg.setEventData(CrossHsIslandActivityPb.buildHsIslandEventData(hsIslandEventReward.getEventData(), false));
            respMsg.setRewardData(CrossHsIslandActivityPb.buildHsIslandRewardData(hsIslandEventReward.getReward()));
            respMsg.setChooseConfigId(result.getChooseConfigId());
            respMsg.setIndex(result.getIndex());
            respMsg.setChooseTrue(result.isChooseTrue());
            respMsg.setActivityId(activity.getActivityId());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_HS_ISLAND_DO_EVENT_CHOOSE_REWARD_FROM_CROSS, respMsg));
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_DO_EVENT_CHOOSE_REWARD, respMsg));
        }
    }

    @Override
    public void sendData() {}
}
